<?php 
class DataBase {

	private static $instance = null;
	
	private $conn = null;
	
	public function __construct() {
		require_once("db_config.inc");
		global $db_config;
		$this->conn = mysql_connect($db_config["server"], $db_config["user"], $db_config["password"]);
		mysql_select_db($db_config["database"], $this->conn);
	}
	
	public function __destruct() {
		mysql_close($this->conn);
	}
	
	public static function create_database($name) {
		require_once("db_config.inc");
		global $db_config;
		$conn = mysql_connect($db_config["server"], $db_config["user"], $db_config["password"]);
		mysql_query("CREATE DATABASE ".$name);
		mysql_close($conn);
	}
	public static function remove_database($name) {
		require_once("db_config.inc");
		global $db_config;
		$conn = mysql_connect($db_config["server"], $db_config["user"], $db_config["password"]);
		mysql_query("DROP DATABASE ".$name);
		mysql_close($conn);
	}
	
	public static function query($sql) {
		if (DataBase::$instance == null) DataBase::$instance = new DataBase();
		if (isset($_SESSION["debug"]) && $_SESSION["debug"]=='yes') {
			global $sql_debug;
			$sql_debug .= "<b>SQL:</b> ".$sql."<br/>";
		}
		$result = mysql_query($sql, DataBase::$instance->conn);
		if (isset($_SESSION["debug"]) && $_SESSION["debug"]=='yes') {
			global $sql_debug;
			$sql_debug.= "<b>Result:</b> Error#".self::last_error_number().": ".self::last_error_message()."<br/>";
		}
		if ($result == false) {
			if (isset($_SESSION["debug"]) && $_SESSION["debug"]=='yes') {
				global $sql_debug;
				$sql_debug.= "<b>Result:</b> Empty<br/>";
			}
			return false;
		}
		$i = 0;
		$arr = array();
		if ($result<>null && !is_bool($result))
		while ($row=mysql_fetch_array($result)) {
			$arr[$i] = $row;
			$i++;
		}
		if (isset($_SESSION["debug"]) && $_SESSION["debug"]=='yes') {
			global $sql_debug;
			$sql_debug.= "<b>Result:</b> ".count($arr)." row(s)<br/>";
		}
		return $arr;
	}
	
	public static function table_name($table) {
		require_once("db_config.inc");
		global $db_config;
		return "`".$db_config["database"]."`.`".$table."`";
	}
	
	public static function select($table, $fields=null, $where=null, $sort=null) {
		$q = "SELECT ";
		if ($fields == null) $q .= "*";
		else { $first = true; foreach ($fields as $name) { if ($first) $first=false; else $q .= ","; $q .= "`".mysql_escape_string($name)."`"; } }
		$q .= " FROM ".DataBase::table_name($table);
		if ($where <> null) {
			$q .= " WHERE ";
			$q .= DataBase::where($where);
		}
		if ($sort <> null)
			$q .= " ORDER BY ".$sort;
		return DataBase::query($q);
	}
	
	public static function delete($table, $where) {
		$q = "DELETE";
		$q .= " FROM ".DataBase::table_name($table);
		if ($where <> null) {
			$q .= " WHERE ";
			$q .= DataBase::where($where);
		}
		return DataBase::query($q);
	}
	
	public static function insert($table, $fields) {
		$query = "INSERT INTO ".DataBase::table_name($table)." (";
		$first = true;
		foreach ($fields as $key => $value) {
			if ($first) $first = false;	else $query .= ",";
			$query .= "`".mysql_escape_string($key)."`";
		}
		$query .= ") VALUES (";
		$first = true;
		foreach ($fields as $key => $value) {
			if ($first)	$first = false;	else $query .= ",";
			$query .= DataBase::getValue($value);
		}
		$query .= ");";
		DataBase::query($query);
		return mysql_insert_id(DataBase::$instance->conn);
	}
	public static function insert_multiples($table, $rows) {
		$query = "INSERT INTO ".DataBase::table_name($table)." (";
		$first = true;
		foreach ($rows[0] as $key => $value) {
			if ($first) $first = false;	else $query .= ",";
			$query .= "`".mysql_escape_string($key)."`";
		}
		$query .= ") VALUES ";
		$firstr = true;
		foreach ($rows as $fields) {
			if ($firstr) $firstr = false; else $query .= ",";
			$query .= "(";
			$first = true;
			foreach ($fields as $key => $value) {
				if ($first)	$first = false;	else $query .= ",";
				$query .= DataBase::getValue($value);
			}
			$query .= ")";
		}
		DataBase::query($query);
	}
	
	public static function update($table, $where, $fields) {
		$query = "UPDATE ".DataBase::table_name($table)." SET ";
		$first = true;
		foreach ($fields as $key=>$value) {
			if ($first) $first = false; else $query .= ",";
			$query .= "`".mysql_escape_string($key)."`=".DataBase::getValue($value)." ";
		}
		$query .= " WHERE ".DataBase::where($where);
		DataBase::query($query);
	}
	
	public static function getValue($value) {
		if ($value === null) return "NULL";
		if (is_bool($value)) return $value?"'1'":"'0'";
		return "'".mysql_escape_string($value)."'";
	}
	
	public static function where($w) {
		$s = "";
		$first = true;
		foreach ($w as $n=>$v) {
			if ($first) $first = false; else $s .= " AND ";
			$s .= "`".mysql_escape_string($n)."`='".mysql_escape_string($v)."'";
		}
		return $s;
	}
	
	public static function last_error_number() {
		return mysql_errno(DataBase::$instance->conn);
	}
	public static function last_error_message() {
		return mysql_error(DataBase::$instance->conn);
	}
	public static function number_of_affected_rows() {
		return mysql_affected_rows(DataBase::$instance->conn);
	}
}
?>